﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace iLangl
{
	public class PackageConfiguration: ConfigurationSection
	{
		private static ConfigurationProperty notauthenticatedPackageFolderPropeperty;
		private static ConfigurationProperty processedPackageFolderProperty;
		private static ConfigurationProperty corruptedPackageFolderProperty;

		private static PackageConfiguration instance;
		private static ConfigurationPropertyCollection properties;

		static PackageConfiguration()
		{
			notauthenticatedPackageFolderPropeperty = new ConfigurationProperty(
			  "notauthenticatedPackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			processedPackageFolderProperty = new ConfigurationProperty(
			  "processedPackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);


			corruptedPackageFolderProperty = new ConfigurationProperty(
			  "corruptedPackageFolder",
			  typeof(String),
			  null,
			  ConfigurationPropertyOptions.IsRequired);

			properties = new ConfigurationPropertyCollection();
			properties.Add(notauthenticatedPackageFolderPropeperty);
			properties.Add(processedPackageFolderProperty);
			properties.Add(corruptedPackageFolderProperty);
		}

		private static object mutex = new object();
		/// <summary>
		/// The instance of the TransportConfiguration.
		/// </summary>
		public static PackageConfiguration Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = ConfigurationManager.GetSection("PackageConfiguration") as PackageConfiguration;
						}
					}
				}
				return instance;
			}
		}

		private String _notauthenticaedPackageFolderPropeperty;
		/// <summary>
		/// Path to the directory where stored packages that failed to pass authentication
		/// </summary>
		public String NotAuthenticaedPackageFolder
		{
			get
			{
				if (!String.IsNullOrEmpty(_notauthenticaedPackageFolderPropeperty))
				{
					return _notauthenticaedPackageFolderPropeperty;
				}

				_notauthenticaedPackageFolderPropeperty = base[notauthenticatedPackageFolderPropeperty] as String;

				if (!Directory.Exists(_notauthenticaedPackageFolderPropeperty))
				{
					Directory.CreateDirectory(_notauthenticaedPackageFolderPropeperty);
				}

				return _notauthenticaedPackageFolderPropeperty;
			}
		}

		private String _processedPackageFolderProperty;
		/// <summary>
		/// Path to the directory of outcomming Packages.
		/// </summary>
		public String ProcessedPackageFolderProperty
		{
			get
			{
				if (!String.IsNullOrEmpty(_processedPackageFolderProperty))
				{
					return _processedPackageFolderProperty;
				}

				_processedPackageFolderProperty = base[processedPackageFolderProperty] as String;

				if (!Directory.Exists(_processedPackageFolderProperty))
				{
					Directory.CreateDirectory(_processedPackageFolderProperty);
				}

				return _processedPackageFolderProperty;
			}
		}

		private String _corruptedPackageFolder = string.Empty;
		/// <summary>
		/// Path to the directory of corrupted Packages.
		/// </summary>
		public String CorruptedPackageFolder
		{
			get
			{
				if (!String.IsNullOrEmpty(_corruptedPackageFolder))
				{
					return _corruptedPackageFolder;
				}

				_corruptedPackageFolder = base[corruptedPackageFolderProperty] as String;

				if (!Directory.Exists(_corruptedPackageFolder))
				{
					Directory.CreateDirectory(_corruptedPackageFolder);
				}
				return _corruptedPackageFolder;
			}
		}

		public void SetSettingsForTesting(
			String notauthenticaedPackageFolderPropeperty,
			String processedPackageFolderProperty,
			String corruptedPackageFolder)
		{
			_notauthenticaedPackageFolderPropeperty = notauthenticaedPackageFolderPropeperty;
			_processedPackageFolderProperty = processedPackageFolderProperty;
			_corruptedPackageFolder = corruptedPackageFolder;
		}

		protected override ConfigurationPropertyCollection Properties
		{
			get
			{
				return properties;
			}
		}
	}
}